
//存在一个按升序排列的链表，给你这个链表的头节点 head ，请你删除所有重复的元素，使每个元素 只出现一次 。 
//
// 返回同样按升序排列的结果链表。 
//
// 
//
// 示例 1： 
//
// 
//输入：head = [1,1,2]
//输出：[1,2]
// 
//
// 示例 2： 
//
// 
//输入：head = [1,1,2,3,3]
//输出：[1,2,3]
// 
//
// 
//
// 提示： 
//
// 
// 链表中节点数目在范围 [0, 300] 内 
// -100 <= Node.val <= 100 
// 题目数据保证链表已经按升序排列 
// 
// Related Topics 链表 
// 👍 563 👎 0

package leetcode.editor.cn;

/**
 * @Author:xpengfei
 * @Date: 2021-04-17 23:27:14
 * @Description：删除排序链表中的重复元素
 */
public class Ques83RemoveDuplicatesFromSortedList {
    public static void main(String[] args) {
        Solution solution = new Ques83RemoveDuplicatesFromSortedList().new Solution();
        ListNode listNode = new Ques83RemoveDuplicatesFromSortedList().new ListNode(1);
        listNode.next = new Ques83RemoveDuplicatesFromSortedList().new ListNode(1);
        listNode.next.next = new Ques83RemoveDuplicatesFromSortedList().new ListNode(1);
        solution.deleteDuplicates(listNode);
    }

    //    Definition for singly-linked list.
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public ListNode deleteDuplicates(ListNode head) {
//            ListNode resultNodeList = new ListNode(0);
//            List nodeList = new ArrayList();
//            ListNode cursor = resultNodeList;
//            while (head != null) {
//                if (!nodeList.contains(head.val)) {
//                    cursor.next = new ListNode(head.val);
//                    cursor = cursor.next;
//                    nodeList.add(head.val);
//                }
//                head = head.next;
//            }
//            return resultNodeList.next;

            if (head == null || head.next == null) {
                return head;
            }
            ListNode current = head;
            while (current.next != null) {
                if (current.val == current.next.val) {
                    current.next = current.next.next;
                } else {
                    current = current.next;
                }
            }
            return head;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}